// PropTab.h : Declaration of the CPropTab
#pragma once
#include "resource.h"       // main symbols
#include <atlctl.h>
#include "EncryptIt_i.h"
#include "Addin.h"
#include "Settings.h"

// CPropTab
class ATL_NO_VTABLE CPropTab :
	public CComObjectRootEx<CComSingleThreadModel>,
	public IDispatchImpl<IPropTab, &IID_IPropTab, &LIBID_EncryptItLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
	public CComCompositeControl<CPropTab>,
	public IPersistStreamInitImpl<CPropTab>,
	public IOleControlImpl<CPropTab>,
	public IOleObjectImpl<CPropTab>,
	public IOleInPlaceActiveObjectImpl<CPropTab>,
	public IViewObjectExImpl<CPropTab>,
	public IOleInPlaceObjectWindowlessImpl<CPropTab>,
	public IPersistStorageImpl<CPropTab>,
	public ISpecifyPropertyPagesImpl<CPropTab>,
	public IQuickActivateImpl<CPropTab>,
	public IDataObjectImpl<CPropTab>,
	public IProvideClassInfo2Impl<&CLSID_PropTab, NULL, &LIBID_EncryptItLib>,
	public IPersistPropertyBagImpl<CPropTab>,
	public ISupportErrorInfo,
	public CComCoClass<CPropTab, &CLSID_PropTab>,
	public IDispatchImpl<PropertyPage, &__uuidof(PropertyPage), &LIBID_EncryptItLib>
{
public:

	CPropTab():m_settings(Settings::Instance())
	{		
		m_bWindowOnly = TRUE;
		CalcExtent(m_sizeExtent);

		m_isPropTabOpened = FALSE;
		m_settings.reset();
	}	

	DECLARE_OLEMISC_STATUS(OLEMISC_RECOMPOSEONRESIZE |
	OLEMISC_CANTLINKINSIDE |
		OLEMISC_INSIDEOUT |
		OLEMISC_ACTIVATEWHENVISIBLE |
		OLEMISC_SETCLIENTSITEFIRST
		)

	DECLARE_REGISTRY_RESOURCEID(IDR_PROPTAB)

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	BEGIN_COM_MAP(CPropTab)
		COM_INTERFACE_ENTRY(IPropTab)
		COM_INTERFACE_ENTRY2(IDispatch,IPropTab)
		COM_INTERFACE_ENTRY(IViewObjectEx)
		COM_INTERFACE_ENTRY(IViewObject2)
		COM_INTERFACE_ENTRY(IViewObject)
		COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless)
		COM_INTERFACE_ENTRY(IOleInPlaceObject)
		COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless)
		COM_INTERFACE_ENTRY(IOleInPlaceActiveObject)
		COM_INTERFACE_ENTRY(IOleControl)
		COM_INTERFACE_ENTRY(IOleObject)
		COM_INTERFACE_ENTRY(IPersistStreamInit)
		COM_INTERFACE_ENTRY2(IPersist, IPersistStreamInit)
		COM_INTERFACE_ENTRY(ISpecifyPropertyPages)
		COM_INTERFACE_ENTRY(IQuickActivate)
		COM_INTERFACE_ENTRY(IPersistStorage)
		COM_INTERFACE_ENTRY(IDataObject)
		COM_INTERFACE_ENTRY(IProvideClassInfo)
		COM_INTERFACE_ENTRY(IProvideClassInfo2)
		COM_INTERFACE_ENTRY(IPersistPropertyBag) 
		COM_INTERFACE_ENTRY(Outlook::PropertyPage)
	END_COM_MAP()

	BEGIN_PROP_MAP(CPropTab)
		PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4)
		PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4)
		// Example entries
		// PROP_ENTRY_TYPE("Property Name", dispid, clsid, vtType)
		// PROP_PAGE(CLSID_StockColorPage)
	END_PROP_MAP()


	BEGIN_MSG_MAP(CPropTab)
		COMMAND_HANDLER(IDC_CHECK_ENABLED, BN_CLICKED, OnBnClickedCheckEnabled)
		COMMAND_HANDLER(IDC_CHECK_ENCITALW, BN_CLICKED, OnBnClickedCheckEncitalw)		
		COMMAND_HANDLER(IDC_CHECK_USEREGEX, BN_CLICKED, OnBnClickedCheckUseregex)
		COMMAND_HANDLER(IDC_CHECK_CHECKSUBJ, BN_CLICKED, OnBnClickedCheckChecksubj)
		COMMAND_HANDLER(IDC_EDIT_MASK, EN_CHANGE, OnEnChangeEditMask)
		CHAIN_MSG_MAP(CComCompositeControl<CPropTab>)
	END_MSG_MAP()

	BEGIN_SINK_MAP(CPropTab)
		//Make sure the Event Handlers have __stdcall calling convention
	END_SINK_MAP()

	STDMETHOD(GetControlInfo)(LPCONTROLINFO lpCI);
	STDMETHOD(SetClientSite)(IOleClientSite *pSite);

	STDMETHOD(OnAmbientPropertyChange)(DISPID dispid)
	{
		if (dispid == DISPID_AMBIENT_BACKCOLOR)
		{
			SetBackgroundColorFromAmbient();
			FireViewChange();
		}
		return IOleControlImpl<CPropTab>::OnAmbientPropertyChange(dispid);
	}

	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid)
	{
		static const IID* arr[] =
		{
			&IID_IPropTab,
		};

		for (int i=0; i<sizeof(arr)/sizeof(arr[0]); i++)
		{
			if (InlineIsEqualGUID(*arr[i], riid))
				return S_OK;
		}
		return S_FALSE;
	}

	DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE)

	enum { IDD = IDD_PROPTAB };

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
	}

	STDMETHOD(GetPageInfo)(BSTR * HelpFile, long * HelpContext);
	STDMETHOD(get_Dirty)(VARIANT_BOOL * Dirty);
	STDMETHOD(Apply)();

private:
	LRESULT OnBnClickedCheckEnabled(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnBnClickedCheckEncitalw(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnBnClickedCheckUseregex(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnBnClickedCheckChecksubj(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnEnChangeEditMask(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);

	VARIANT_BOOL isDirty()
	{
		return m_settings.isDirty();		
	}

	CComPtr<Outlook::PropertyPageSite> m_pPropPageSite; 
	VARIANT_BOOL m_isPropTabOpened, m_isLoading;
	Settings &m_settings;
};

OBJECT_ENTRY_AUTO(__uuidof(PropTab), CPropTab)
